cmake_policy(SET CMP0017 NEW)

cmake_minimum_required(VERSION 2.8)

set(APP_NAME mygame6)
project(${APP_NAME})


set(COCOS2D_ROOT ${CMAKE_SOURCE_DIR}/cocos2d)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${COCOS2D_ROOT}/cmake/Modules/")
include(CocosBuildHelpers)



# libcocos2d
set(BUILD_CPP_TESTS OFF CACHE BOOL "turn off build cpp-tests")
set(BUILD_LUA_LIBS OFF CACHE BOOL "turn off build lua-tests")
add_subdirectory(${COCOS2D_ROOT})

# Some macro definitions
if (WINDOWS)
    if (BUILD_SHARED_LIBS)
        ADD_DEFINITIONS(-D_USRDLL -D_EXPORT_DLL_ -D_USEGUIDLL -D_USREXDLL -D_USRSTUDIODLL)
    else ()
        ADD_DEFINITIONS(-DCC_STATIC)
    endif ()

    ADD_DEFINITIONS(-DCOCOS2DXWIN32_EXPORTS -D_WINDOWS -DWIN32 -D_WIN32)
    set(PLATFORM_FOLDER win32)
elseif (MACOSX OR APPLE)
    ADD_DEFINITIONS(-DCC_TARGET_OS_MAC)
    ADD_DEFINITIONS(-DUSE_FILE32API)
    set(PLATFORM_FOLDER mac)
elseif (LINUX)
    ADD_DEFINITIONS(-DLINUX)
    set(PLATFORM_FOLDER linux)
elseif (ANDROID)
    ADD_DEFINITIONS(-DUSE_FILE32API)
    set(PLATFORM_FOLDER android)
else ()
    message(FATAL_ERROR "Unsupported platform, CMake will exit")
endif ()


# Compiler options
if (MSVC)
    ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS
            -wd4251 -wd4244 -wd4334 -wd4005 -wd4820 -wd4710
            -wd4514 -wd4056 -wd4996 -wd4099)
else ()
    set(CMAKE_C_FLAGS_DEBUG "-g -Wall -DCOCOS2D_DEBUG=1")
    set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-exceptions -std=c99")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -std=c++11 -Wno-deprecated-declarations -Wno-reorder")
    if (CLANG)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
    endif ()
endif (MSVC)


set(PLATFORM_SPECIFIC_SRC)
set(PLATFORM_SPECIFIC_HEADERS)
if (MACOSX OR APPLE)
    set(PLATFORM_SPECIFIC_SRC
            proj.ios_mac/mac/main.cpp
            )

elseif (LINUX) #assume linux
    set(PLATFORM_SPECIFIC_SRC
            proj.linux/main.cpp
            )
elseif (WIN32)
    set(PLATFORM_SPECIFIC_SRC
            proj.win32/main.cpp
            )
    set(PLATFORM_SPECIFIC_HEADERS
            proj.win32/main.h
            proj.win32/resource.h
            )
endif ()

#cmake使用
#http://blog.atime.me/note/cmake.html

#项目依赖头文件引入,注意编译ios mac 将对应的依赖文件在xcode中处理
include_directories(
        ${COCOS2D_ROOT}/external
        ${COCOS2D_ROOT}/external/tinyxml2
        ${COCOS2D_ROOT}/external/glfw3/include/mac
        ${COCOS2D_ROOT}/cocos
        ${COCOS2D_ROOT}/cocos/editor-support
        lib/protobuf/include
        src
)


#查找指定路径下的源文件并保存在指定的变量名中
#src_path , var
aux_source_directory(src app_src)
aux_source_directory(src/scene scence_src)
aux_source_directory(src/proto proto_src)
aux_source_directory(src/manager manager_src)
aux_source_directory(src/gameObject gameobj_src)
aux_source_directory(src/controller controller_src)
aux_source_directory(src/commons commons_src)
aux_source_directory(src/commons/net net_src)
aux_source_directory(src/commons/sound sound_src)
aux_source_directory(src/layer layer_src)

#使用net_src中的源文件生成静态库net
#add_library(net STATIC ${net_src})


add_executable(
        ${APP_NAME}

        ${PLATFORM_SPECIFIC_SRC}

        ${app_src}
        ${scence_src}
        ${proto_src}
        ${manager_src}
        ${gameobj_src}
        ${controller_src}
        ${commons_src}
        ${net_src}
        ${sound_src}
        ${layer_src}
)


#外联静态库
set(proto_lib ${CMAKE_SOURCE_DIR}/lib/protobuf)
target_link_libraries(${APP_NAME} cocos2d ${proto_lib}/libprotobuf.a)


set(APP_BIN_DIR "${CMAKE_SOURCE_DIR}/bin")

set_target_properties(${APP_NAME} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${APP_BIN_DIR}")

if (WIN32)
    #also copying dlls to binary directory for the executable to run
    pre_build(${APP_NAME}
            COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
            COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}/Resources
            COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/gles/prebuilt/glew32.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}
            COMMAND ${CMAKE_COMMAND} -E copy ${COCOS2D_ROOT}/external/win32-specific/zlib/prebuilt/zlib1.dll ${APP_BIN_DIR}/${CMAKE_BUILD_TYPE}
            )
else ()
    pre_build(${APP_NAME}
            COMMAND ${CMAKE_COMMAND} -E remove_directory ${APP_BIN_DIR}/Resources
            COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Resources ${APP_BIN_DIR}
            )

endif ()